package bsf.baseservice.businessmq.dal;


import bsf.baseservice.businessmq.model.tb_consumer_model;
import bsf.db.DbConn;
import bsf.util.ConvertUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class tb_consumer_dal
{
	public final boolean deleteNotOnLineByClientID(DbConn PubConn, int consumerclientid, int maxtimeoutsenconds)
	{
			String Sql = "delete from tb_consumer where consumerclientid=? and DATEDIFF(S,lastheartbeat,getdate())>?";

			int rev = PubConn.executeSql(Sql, new Object[]{consumerclientid,maxtimeoutsenconds});
			return true;
	}

	public final boolean deleteClient(DbConn PubConn, int consumerclientid, long tempid)
	{
			String Sql = "delete from tb_consumer where consumerclientid=? and tempid=?";
			int rev = PubConn.executeSql(Sql, new Object[]{consumerclientid,tempid});
			return true;
	}

	public final boolean clientHeatbeat(DbConn PubConn, int consumerclientid, long tempid)
	{
			String Sql = "update tb_consumer WITH (ROWLOCK) set lastheartbeat=getdate() where consumerclientid=? and tempid=?";
			int rev = PubConn.executeSql(Sql, new Object[]{consumerclientid,tempid});
			return true;
	}

	public final ArrayList<Integer> getRegisterPartitionIndexs(DbConn PubConn, int consumerclientid)
	{
		java.util.ArrayList<Integer> rs = new java.util.ArrayList<Integer>();
		String Sql = "select s.partitionindexs from tb_consumer s WITH(NOLOCK) where s.consumerclientid=?";
		List<Map<String, Object>> ds= PubConn.executeList(Sql.toString(), new Object[]{consumerclientid});
			if (ds != null && ds.size() > 0)
			{
				for (Map<String, Object> dr : ds)
				{
					String partitionindexs = String.valueOf(dr.get("partitionindexs"));
					String[] indexs = StringUtils.strip(partitionindexs, ",").split("[,]", -1);
					for (String index : indexs)
					{
						if (!StringUtils.isEmpty(index) &&!rs.contains(Integer.parseInt(index)))
						{
							rs.add(Integer.parseInt(index));
						}
					}
				}
			}
			return rs;

	}

	public boolean add2(DbConn PubConn, tb_consumer_model model)
	{
					//��������ʱid(������������Ψһ,Guidתlong)
					//������clinet��id
					//֧�ֵķ���˳���(֧�ֶ��˳���)
					//�ͻ�������
					////�������ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
					//new ProcedureParameter("@lastheartbeat",    model.lastheartbeat),
					////��һ�θ���ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
					//new ProcedureParameter("@lastupdatetime",    model.lastupdatetime),
					////�ͻ��˴���ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
					//new ProcedureParameter("@createtime",    model.createtime)

			int rev = PubConn.executeSql("insert into tb_consumer(" +
					"tempid,consumerclientid,partitionindexs,clientname,lastheartbeat,lastupdatetime,createtime) " +
					"values(?,?,?,?,getdate(),getdate(),getdate())",
					new Object[]{model.tempid,model.consumerclientid,model.partitionindexs,model.clientname});
			return rev == 1;

	}

	public tb_consumer_model get(DbConn PubConn, long tempid, int consumerclientid)
	{
			StringBuilder stringSql = new StringBuilder();
			stringSql.append("select s.* from tb_consumer s WITH(NOLOCK) where s.tempid=? and consumerclientid=?");
		List<Map<String, Object>> ds = PubConn.executeList(stringSql.toString(), new Object[]{tempid,consumerclientid});
			if (ds != null && ds.size() > 0)
			{
				return createModel(ds.get(0));
			}
			return null;
	}



	public tb_consumer_model createModel(Map<String, Object> dr)
	{
		tb_consumer_model o = new tb_consumer_model();

		//
		if (dr.containsKey("id"))
		{
			o.id= ConvertUtil.objToInt(dr.get("id"));
		}
		//��������ʱid(������������Ψһ,Guidתlong)
		if (dr.containsKey("tempid"))
		{
			o.tempid=ConvertUtil.objToInt64(dr.get("tempid"));
		}
		//������clinet��id
		if (dr.containsKey("consumerclientid"))
		{
			o.consumerclientid=ConvertUtil.objToInt(dr.get("consumerclientid"));
		}
		//֧�ֵķ���˳���(֧�ֶ��˳���)
		if (dr.containsKey("partitionindexs"))
		{
			o.partitionindexs=ConvertUtil.nullToStr(dr.get("partitionindexs"));
		}
		//�ͻ�������
		if (dr.containsKey("clientname"))
		{
			o.clientname= ConvertUtil.nullToStr(dr.get("clientname"));
		}
		//�������ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
		if (dr.containsKey("lastheartbeat"))
		{
			o.lastheartbeat = ConvertUtil.objToDateTime(dr.get("lastheartbeat"));
		}
		//��һ�θ���ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
		if (dr.containsKey("lastupdatetime"))
		{
			o.lastupdatetime=ConvertUtil.objToDateTime(dr.get("lastupdatetime"));
		}
		//�ͻ��˴���ʱ��(�Ե�ǰ��ʱ��Ϊ׼)
		if (dr.containsKey("createtime"))
		{
			o.createtime=ConvertUtil.objToDateTime(dr.get("createtime"));
		}
		return o;
	}
}